


Page({
  data: {


		txt:[
			{
				tit:'活动信息'
			},
			{
				tit:'活动配置'
			},
			{
				tit:'推广投放'
			},
			{
				tit:'完成'
			}
		],

		taskType:[
			{txt:'每日签到',type:'SIGN'},
			{txt:'关注店铺',type:'FOLLOW'},
			{txt:'加入会员',type:'MEMBER'},
			{txt:'浏览商品',type:'VIEW'},
			{txt:'收藏商品',type:'COLLECT'},
			{txt:'分享活动',type:'SHARE'},
		],

		// 控制进程
    state:1,    // 0-活动信息 -- 1-活动配置 -- 2-推广投放 -- 3-完成

    // 用户版本
		versionNo:0,   //1-基础版本  --- 2-中级版本  ---3-高级版本   目前只有1和3
		
		orderLevel:0

  },
  


	prev(){

		this.setData({
			state:this.data.state - 1 < 0 ? 0 : this.data.state - 1
		})

  },
  
	next(){

    if(this.data.state==0){
      

      this.saveData();

    }else if(this.data.state == 1){

      getApp().edit = ''    //清空是否编辑跳转过去的标识
      
      let ii = this.data.orderLevel

      if(ii == 1){
        //日常
        this.cjtask();

      }else if(ii == 3){

        //大促
        this.gjtask()
      }else if(ii == 4){
        
        //会员运营
        this.member1();

      }
		}


    
  },
  //会员运营
  member1(){
    let data = ''
		data = this.member.data
	
			let aTask = JSON.parse(JSON.stringify(data.taskList));

			let aShare = []

			aTask.map((v,i) => {
				this.data.taskType.map((a,idx) => {
					if(v.tit == a.txt){
            v.type = a.type
					}
					v.name = v.tit
					v.orderIndex = i
					v.dayLimitTime = v.num
          // v.prize_num = v.prizeNum
          
        })
        
        this.data.taskIds.map((x,y) => {
          if(x.id && x.type == v.type){
            v.id = x.id
          }
        })
        
			})

			for(let ii in aTask){
				if(aTask[ii].tit == '分享活动'){
					aShare = aTask[ii]
					break;
				}
			}

//--------------------------------------------------------------------------------------
		  aTask.push({
        id:this.data.taskDayVos[0] ? this.data.taskDayVos[0].id : '',
				name:'用户每天增加次数',
				dayLimitTime:data.userDayGameTime ? data.userDayGameTime : 1,
        orderIndex:20,
        prizeNum:data.userDayGameTime ? data.userDayGameTime : 1,
				type:'USER_DAY_ADD'
			})

			aTask.push({
        id:this.data.taskDayVos[1] ? this.data.taskDayVos[1].id : '' ,
				name:'老用户每天增加次数',
				dayLimitTime:data.oldUserDayGameTime ? data.oldUserDayGameTime : 1,
        orderIndex:21,
        prizeNum:data.oldUserDayGameTime ? data.oldUserDayGameTime : 1,
				type:'OLD_USER_DAY_ADD'
			})

			aTask.push({
        id:this.data.taskDayVos[2] ? this.data.taskDayVos[2].id : '',
				name:'用户关注每天增加的次数',
        dayLimitTime:data.userFollowDayGameTime ? data.userFollowDayGameTime : 0,
        prizeNum:data.userFollowDayGameTime ? data.userFollowDayGameTime : 0,
        orderIndex:22,
				type:'USER_FOLLOW_DAY_ADD'
      })

      //!------------------------------------------------------------//


     console.log("这里是处理过的任务=======>",aTask,aShare);

			let prizeData = []

			data.prizeData.map((v,i) => {
				let aaPrize = {
							dayPrizeLimitCount : v.dayNum,
							num : v.count,
							prizeImage : v.url,
							prizeName:v.name,
							rate:v.probability,
							type:v.type == '实物奖' ? 1 : 2,
              difficulty:v.diff == '高' ? 3 : v.diff == '中' ? 2 : 1,
              id:v.id,
              bindNumIid : v.bindNumIid,
              ename:v.ename ? v.ename : ''
						}
				
						prizeData.push(aaPrize)
				
      })
      

			let listPrizeData = []

			data.listPrize.map((v,i) => {
				let aaPrize = {
							prizeImage:v.url,
							prizeName:v.name,
							ranking:v.num,
              type:v.typ == '实物奖' ? 1 : 2,
              id:v.id,
              bindNumIid : v.bindNumIid,
              ename:v.ename ? v.ename : ''
						}
				
						listPrizeData.push(aaPrize)
				
			})

			data.endSelectShopData.map((v,i) => {
				v.productId = v.num_iid
				v.logo = v.pic_url
			})

      console.info(data.endSelectShopData)

      let rr = data.roleText.split('[回车]').join('\n')
      
		let o = {

			activityId:this.data.acId,
			levelInfos: data.diffList,
			roleText:rr,
			oldUserDayGameTime:data.oldUserDayGameTime,
			userFollowDayGameTime:data.userFollowDayGameTime,
			userDayGameTime:data.userDayGameTime,
			tasks :aTask,
			pkPrizes:prizeData,
      topPrizes:listPrizeData,
      scoreToGameTime:data.integral>0?data.integral:1,
      orderLevel:4,
			collectProducts:data.endSelectShopData,
			activityShare:data.activityShare
    
    }
    
    console.log("huiyuan=======>",o)

		this.upLoadActivity(o)
    
  },

	 //高级
  gjtask(){
    
    let data = ''
		// data = this.optionsPage.$page.optionCpt.data
    data = this.optionCpt.data
    
    //console.info("整个数据",data);
	
    let aTask = JSON.parse(JSON.stringify(data.taskList));

    let aShare = []

    aTask.map((v,i) => {
      this.data.taskType.map((a,idx) => {
        if(v.tit == a.txt){
          v.type = a.type
        }
        v.name = v.tit
        v.orderIndex = i
        v.dayLimitTime = v.num
        // v.prize_num = v.prizeNum
        
      })

      this.data.taskIds.map((x,y) => {
        if(x.id && x.type == v.type){
          v.id = x.id
        }
      })
    })

    // console.info("6666",aTask)

    // console.info("===============>", this.data.taskIds)

    for(let ii in aTask){
      if(aTask[ii].tit == '分享活动'){
        aShare = aTask[ii]
        break;
      }
    }

    aTask.push({
        id:this.data.taskDayVos[0] ? this.data.taskDayVos[0].id : '',
				name:'用户每天增加次数',
				dayLimitTime:data.userDayGameTime ? data.userDayGameTime : 1,
        orderIndex:20,
        prizeNum:data.userDayGameTime ? data.userDayGameTime : 1,
				type:'USER_DAY_ADD'
			})

			aTask.push({
        id:this.data.taskDayVos[1] ? this.data.taskDayVos[1].id : '',
				name:'老用户每天增加次数',
				dayLimitTime:data.oldUserDayGameTime ? data.oldUserDayGameTime : 1,
        orderIndex:21,
        prizeNum:data.oldUserDayGameTime ? data.oldUserDayGameTime : 1,
				type:'OLD_USER_DAY_ADD'
			})

			aTask.push({
        id:this.data.taskDayVos[2] ? this.data.taskDayVos[2].id : '',
				name:'用户关注每天增加的次数',
        dayLimitTime:data.userFollowDayGameTime ? data.userFollowDayGameTime : 1,
        prizeNum:data.userFollowDayGameTime ? data.userFollowDayGameTime : 1,
        orderIndex:22,
				type:'USER_FOLLOW_DAY_ADD'
      })

      //!------------------------------------------------------------//

     console.log("这里是处理过的任务=======>",aTask,aShare);

			let prizeData = []

			data.prizeData.map((v,i) => {
				let aaPrize = {
							dayPrizeLimitCount : v.dayNum,
							num : v.count,
							prizeImage : v.url,
							prizeName:v.name,
							rate:v.probability,
							type:v.type == '实物奖' ? 1 : 2,
              difficulty:v.diff == '高' ? 3 : v.diff == '中' ? 2 : 1,
              id:v.id,
              bindNumIid : v.bindNumIid,
              ename:v.ename ? v.ename : ''
						}
				
						prizeData.push(aaPrize)
				
      })
      
      console.info("prizeData",this.data.prizeData);

			let listPrizeData = []

			data.listPrize.map((v,i) => {
        
				let aaPrize = {
					prizeImage:v.url,
					prizeName:v.name,
					ranking:v.num,
          type:v.typ == '实物奖' ? 1 : 2,
          id:v.id,
          bindNumIid : v.bindNumIid,
          ename:v.ename ? v.ename : ''
				}
				
				listPrizeData.push(aaPrize)
				
      })
      
      console.info("listPrize",data.prizeData);

			data.endSelectShopData.map((v,i) => {
				v.productId = v.num_iid
				v.logo = v.pic_url
			})

      console.info(data.endSelectShopData)

      let rr = data.roleText.split('[回车]').join('\n')

		let o = {

			activityId:this.data.acId,
      levelInfos: data.diffList,
			roleText:rr,
			oldUserDayGameTime:data.oldUserDayGameTime,
			userFollowDayGameTime:data.userFollowDayGameTime,
			userDayGameTime:data.userDayGameTime,
			tasks :aTask,
      pkPrizes:prizeData,
      topPrizes:listPrizeData,    //这个是空的
      scoreToGameTime:data.integral>0?data.integral:1,
      orderLevel:3,
			collectProducts:data.endSelectShopData,
      //activityShare:data.activityShare,
      
      activityShare:{
				content:aShare.shareTextarea,
				logo:aShare.shareImg ? aShare.shareImg : '',
				title:aShare.title
			}
    
    }
    
    console.log("********************************>",o)

		this.upLoadActivity(o)

  },


  //初级
  cjtask(){
    
			let data = ''

			console.info('thithitithtitithith=======>',this)
			data = this.lowOption.data

			let aTask = JSON.parse(JSON.stringify(data.taskList));

			aTask.map((v,i) => {
				this.data.taskType.map((a,idx) => {
					if(v.tit == a.txt){
            v.type = a.type
					}
					v.dayLimitTime = v.num
					v.name = v.tit
          v.orderIndex = i
          // v.prize_num = v.prizeNum
          
        })
        
        this.data.taskIds.map((x,y) => {
          if(x.id && x.type == v.type){
            v.id = x.id
          }
        })

      })



      aTask.push({
        id:this.data.taskDayVos[0] ? this.data.taskDayVos[0].id : '',
				name:'用户每天增加次数',
				dayLimitTime:data.userDayGameTime ? data.userDayGameTime : 1,
        orderIndex:20,
        prizeNum:data.userDayGameTime ? data.userDayGameTime : 1,
				type:'USER_DAY_ADD'
			})

			aTask.push({
        id:this.data.taskDayVos[1] ? this.data.taskDayVos[1].id : '',
				name:'老用户每天增加次数',
				dayLimitTime:data.oldUserDayGameTime ? data.oldUserDayGameTime : 0,
        orderIndex:21,
        prizeNum:data.oldUserDayGameTime ? data.oldUserDayGameTime : 0,
				type:'OLD_USER_DAY_ADD'
			})

			aTask.push({
        id:this.data.taskDayVos[2] ? this.data.taskDayVos[2].id : '',
				name:'用户关注每天增加的次数',
        dayLimitTime:data.userFollowDayGameTime ? data.userFollowDayGameTime : 0,
        prizeNum:data.userFollowDayGameTime ? data.userFollowDayGameTime : 0,
        orderIndex:22,
				type:'USER_FOLLOW_DAY_ADD'
      })
      
      //console.info(aTask)

      let prizeData = []
      
   

			data.prizeData.map((v,i) => {
				let aaPrize = {
							dayPrizeLimitCount : v.dayNum,
							num : v.count,
							prizeImage : v.url,
							prizeName:v.name,
							rate:v.probability,
							type:v.type == '实物奖' ? 1 : 2,
							difficulty:v.diff == '高' ? 3 : v.diff == '中' ? 2 : 1,
              id:v.id,
              bindNumIid : v.bindNumIid,
              ename:v.ename ? v.ename : ''
						}
				
						prizeData.push(aaPrize)
				
			})

      let listPrizeData = []
      
         //console.log("0000000",data.listPrize)

			data.listPrize.map((v,i) => {
				let aaPrize = {
							prizeImage:v.url,
							prizeName:v.name,
							ranking:v.num,
							type:v.typ == '实物奖' ? 1 : 2,
              id:v.id,
              bindNumIid : v.bindNumIid,
              ename:v.ename ? v.ename : ''
						}
				
						listPrizeData.push(aaPrize)
				
      })

      
     let rr = data.roleText.split('[回车]').join('\n')
      
      
		let o = {

			activityId:this.data.acId,
			levelInfos: data.diffList,
			roleText:rr,
			oldUserDayGameTime:data.oldUserDayGameTime,
			userFollowDayGameTime:data.userFollowDayGameTime,
			userDayGameTime:data.userDayGameTime,
			tasks :aTask,
			pkPrizes:prizeData,
      topPrizes:listPrizeData,
      scoreToGameTime:data.integral>0?data.integral:1,
      orderLevel:1,
      activityShare:data.activityShare
    
    }

    console.log("8888888888=============",o);

		this.upLoadActivity(o)
  },
	
	
	upLoadActivity(o){

		console.info(JSON.stringify(o))

		my.showLoading();

		getApp().cloud.application.httpRequest({
      'path' : '/api/b/saveActionSetting', 
      'method':'POST',
      'headers':{ "Content-Type":"application/json"},
      'body':o,
      }).then(res => {

        my.hideLoading();
        
        if(this.lowOption){
          this.lowOption.onSubmit();
        }

        if(this.optionCpt){
          this.optionCpt.onSubmit();
        }

        if(this.member){
          this.member.onSubmit();
        }

				console.info('返回的数据',res)

        if(res.code!=200){

          my.alert({content:res.msg})
          
        }else{

					my.navigateBack()
          
        }
        

    })

  },


  onShow(){

  },

  onHide() {

    // getApp().edit = ''

	},
	
	cancel(){

    getApp().edit = ''

    my.navigateBack();
    
	},

	onLoad(query){


    
      getApp().actStart = '',
      getApp().actEnd = ''


    // 做下判断显示哪个
		console.log("djfksdjkfjsd",query.type,query.orderLevel,query.id)
		
		console.info(this)

		// this.lowOption.


		this.setData({
			acId:query.id,
			orderLevel:query.orderLevel
		})


		setTimeout(() => {
			if(query.orderLevel == 1){
			
				this.optionsPage.jh([
						{
							tit:"日常方案",
							txt:'适用于简单营销需求，引导消费者关注店铺或者加入会员，日常新品推广',
							star:[1,2]
				}])
				this.optionsPage.setData({
					active:0
				})

			}else if(query.orderLevel == 3){

				this.optionsPage.jh([
						{
								tit:"大促方案",
								txt:'适用于大促场景，引导消费者收藏大促商品或者分享邀请好友，拉新裂变，提升商品IPV',
								star:[1,2,3]
						}
				])
				this.optionsPage.setData({
					active:1
        })
        
			}else if(query.orderLevel == 4){

        this.optionsPage.jh([
						{
							tit:"会员运营",
              txt:'适用于会员运营，会员招募，配置会员专属权益，提升会员运营效果',
              star:[1,2]
						}
				])
				this.optionsPage.setData({
					active:2
        })
        
      }
		}, 10);

    console.info("this",this)

    getApp().cloud.application.httpRequest({
      'path' : `/api/b/getTasks`, 
      'method':'POST',
      'headers':{ "Content-Type":"application/json"},
      'body':{ id : query.id },
    }).then(res => { 

			console.log("活动回显======================>",res.data);
      //console.log("活动回显======================>",JSON.stringify(res.data));
      
      //return
			
			let data = res.data

      if(res.code!=200){

        my.alert({ content : res.msg });

      }else{

        this.setData({
          taskIds:data.tasks
        })

        if(query.orderLevel == 1){
					
					data.pkPrizes.map((v,i) => {
						v.name = v.prizeName
						v.url = v.prizeImage
						v.diff = v.difficulty == 1 ? '低' : v.difficulty == 2 ? '中' : '高'
						v.type = v.type == 1 ? '实物奖' : '虚拟奖'
						v.probability = v.rate
						v.dayNum = v.dayPrizeLimitCount
						v.count = v.num
						v.id = v.id
					})
					
					data.topPrizes.map((v,i) => {
						v.name = v.prizeName
						v.url = v.prizeImage
						v.num = v.ranking
						v.type = v.type == 1 ? '实物奖' : '虚拟奖'
						v.id = v.id
          })

          // data.tasksmap((v,i) => {
          //   v.id = 
          // })

          
          //规则回显

          //console.log(data.roleText);

          let rr = data.roleText.split('[回车]').join('\n')

          this.setData({
            taskDayVos:data.taskDayVos
          })

          data.tasks.map((v,i) => {

            v.num = v.dayLimitTime
            
          })

          
					this.lowOption.setData({

						diffList:data.levelInfos,

						// userDayGameTime:data.userDayGameTime,
						// oldUserDayGameTime:data.oldUserDayGameTime,
            // userFollowDayGameTime:data.userFollowDayGameTime,
            userDayGameTime: data.taskDayVos[0] ? data.taskDayVos[0].num : 0,
						oldUserDayGameTime: data.taskDayVos[1] ? data.taskDayVos[1].num : 0,
            userFollowDayGameTime: data.taskDayVos[2] ? data.taskDayVos[2].num : 0,
            
						roleText:rr,

						taskList:data.tasks,


						prizeData:data.pkPrizes,

            listPrize:data.topPrizes,

            activityShare:data.activityShare
            
						
					})
				}else if(query.orderLevel == 3){
					if(data.pkPrizes){
						data.pkPrizes.map((v,i) => {
							v.name = v.prizeName
							v.url = v.prizeImage
							v.diff = v.difficulty == 1 ? '低' : v.difficulty == 2 ? '中' : '高'
							v.type = v.type == 1 ? '实物奖' : '虚拟奖'
							v.probability = v.rate
							v.dayNum = v.dayPrizeLimitCount
							v.count = v.num
						})
					}
					
					if(data.topPrizes){
						data.topPrizes.map((v,i) => {
							v.name = v.prizeName
							v.url = v.prizeImage
							v.num = v.ranking
							v.type = v.type == 1 ? '实物奖' : '虚拟奖'
						})
          }

          // if(data.activityShare){
          //   data.activityShare.map((v)=>{
          //     v.activityId = v.activityId
          //     v.content = v.content
          //     v.createTime = v.createTime
          //     v.id = v.id
          //     v.logo = v.logo
          //     v.title = v.title
          //   })
          // }
          
          //规则回显
          let rr = data.roleText.split('[回车]').join('\n')

          data.tasks.map((v,i) => {
            if(v.type == 'SHARE'){
              v.shareTextarea = data.activityShare.content
              v.shareImg = data.activityShare.logo
              v.title = data.activityShare.title
            }

            v.num = v.dayLimitTime

          })







          



          // data.tasks.map((v,i) => {
          //   if(v.type == 'SHARE'){
          //     v.shareTextarea = data.activityShare.content
          //     v.shareImg = data.activityShare.logo
          //     v.title = data.activityShare.title
          //   }

          //   v.num = v.dayLimitTime

          // })

          // let result1 = ''

          // for (let i = 0; i < rr.length-4; i++) {
          //   let c = rr.substr(i, 4);
          //   if (c == "[回车]")
          //     result1 = result1 + "\n";
          //   else
          //     result1 = result1 + c
          // }

          console.info("这个值",data.topPrizes)

          console.info("this===========>",this)

          this.setData({
            taskDayVos:data.taskDayVos
          })



          setTimeout(()=>{
            this.optionCpt.setData({

              diffList:data.levelInfos,

              // userDayGameTime:data.userDayGameTime,
              // oldUserDayGameTime:data.oldUserDayGameTime,
              // userFollowDayGameTime:data.userFollowDayGameTime,
              endSelectShopData:data.collectProducts,
              userDayGameTime: data.taskDayVos[0] ? data.taskDayVos[0].num : 0,
              oldUserDayGameTime: data.taskDayVos[1] ? data.taskDayVos[1].num : 0,
              userFollowDayGameTime: data.taskDayVos[2] ? data.taskDayVos[2].num : 0,

              roleText:rr,

              taskList:data.tasks,

              prizeData:data.pkPrizes,

              listPrize:data.topPrizes,

              activityShare:data.activityShare

            })
          },800)

				

          setTimeout(()=>{
            console.info('==============>dddd',this.optionCpt)
          },1000)
          

          

				}else if(query.orderLevel == 4){
          
          if(data.pkPrizes){
						data.pkPrizes.map((v,i) => {
							v.name = v.prizeName
							v.url = v.prizeImage
							v.diff = v.difficulty == 1 ? '低' : v.difficulty == 2 ? '中' : '高'
							v.type = v.type == 1 ? '实物奖' : '虚拟奖'
							v.probability = v.rate
							v.dayNum = v.dayPrizeLimitCount
							v.count = v.num
						})
					}
					
					if(data.topPrizes){
						data.topPrizes.map((v,i) => {
							v.name = v.prizeName
							v.url = v.prizeImage
							v.num = v.ranking
							v.type = v.type == 1 ? '实物奖' : '虚拟奖'
						})
          }
          
          //规则回显
          let rr = data.roleText.split('[回车]').join('\n');

          // data.tasks.map((v,i) => {
          //   if(v.type == 'SHARE'){
          //     v.shareTextarea = data.activityShare.content
          //     v.shareImg = data.logo
          //     v.title = data.title
          //   }
          // })

          data.tasks.map((v,i) => {

            v.num = v.dayLimitTime
            
          })



          // let result1 = ''

          // for (let i = 0; i < rr.length-4; i++) {
          //   let c = rr.substr(i, 4);
          //   if (c == "[回车]")
          //     result1 = result1 + "\n";
          //   else
          //     result1 = result1 + c
          // }

          this.setData({
            taskDayVos:data.taskDayVos
          })

					this.member.setData({

						diffList:data.levelInfos,

						// userDayGameTime:data.userDayGameTime,
						// oldUserDayGameTime:data.oldUserDayGameTime,
            // userFollowDayGameTime:data.userFollowDayGameTime,
            userDayGameTime: data.taskDayVos[0] ? data.taskDayVos[0].num : 0,
						oldUserDayGameTime: data.taskDayVos[1] ? data.taskDayVos[1].num : 0,
            userFollowDayGameTime: data.taskDayVos[2] ? data.taskDayVos[2].num : 0,

						roleText:rr,

						taskList:data.tasks,

						prizeData:data.pkPrizes,

            listPrize:data.topPrizes,
            
            activityShare:data.activityShare
						
          })
          
          

        } 

      }
    })

    // this.getVersion();      //查询用户订购版本  用于编辑跳转低高级
    // this.getSkinTheme();    //皮肤主题
    // this.getNow();      

		// getApp().cloud.application.httpRequest({
    //   'path' : '/api/b/user/getMerchantOnSale', 
    //   'method':'GET',
    //   'headers':{ "Content-Type":"application/json"},
    //   'body':{},
    //   }).then(ress => {
    //     console.info("11111",ress)
    // })

  },

  
});
